using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Globalization;


namespace GSync
{
    /// <summary>
    /// This is simple logger class.
    /// </summary>
    public class TextFileLogger : ILogger
    {
        public TextFileLogger()
        {
        }

        public TextFileLogger(string fileName)
        {
            FilePath = fileName;
        }
        /// <summary>
        /// Immediately append log entry to file
        /// </summary>
        /// <param name="o"></param>
        /// <param name="category"></param>
        public void Write(object o, string category)
        {
            String logEntry = string.Format(CultureInfo.CurrentCulture, 
                                            "====={0} at {1}=====\r\n{2}", 
                                            new object[] { category, DateTime.Now.ToString("G"), o.ToString() });

            OutputStream.Write(logEntry);
        }

      
        public string FilePath { get; set; }

        private StreamWriter _OutputStream;
        protected StreamWriter OutputStream
        {
            get
            {
                if (_OutputStream == null)
                {
                    _OutputStream = File.CreateText(FilePath);
                }

                return _OutputStream;
            }
        }

        #region ILogger Members


        public void Flush()
        {
            OutputStream.Flush();
        }

        #endregion

        #region IDisposable Members
        private bool IsDisposed = false;
        public void Dispose()
        {
            if (!IsDisposed)
            {
                IsDisposed = true;
                Flush();
                if (_OutputStream != null)
                {
                    
                    _OutputStream.Dispose();
                }
            } 
        }

        #endregion
    }
}
